package wails_utils

import (
	"crypto/md5"
	"encoding/hex"
	"errors"
	"github.com/shirou/gopsutil/cpu"
	"github.com/shirou/gopsutil/host"
)

// GetSoftInfo 获取硬件信息 加密
func GetSoftInfo(name string) (string, error) {
	boardRes, err := ReadBoard()
	if err != nil {
		return "", err
	}
	cpuRes, err := ReadCpu()
	if err != nil {
		return "", err
	}

	hash := md5.Sum([]byte(cpuRes + boardRes + name))
	return hex.EncodeToString(hash[:]), nil
}

// ReadBoard 获取主板信息
func ReadBoard() (string, error) {
	hostInfo, err := host.Info()
	if err != nil {
		return "", err
	}

	res := hostInfo.KernelVersion + hostInfo.KernelArch + hostInfo.HostID + hostInfo.Platform
	return res, nil
}

func ReadCpu() (string, error) {
	// 获取CPU信息
	cpuInfo, err := cpu.Info()
	if err != nil {
		return "", err
	}
	if len(cpuInfo) == 0 {
		return "", errors.New("获取硬件信息失败")
	}

	res := cpuInfo[0].VendorID + cpuInfo[0].Family + cpuInfo[0].PhysicalID + cpuInfo[0].ModelName
	return res, nil
}
